package com.lvmama.pangolin.hotel.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lvmama.pangolin.mybatis.mapper.hotel.HotelCommodityDetailMapper;
import com.lvmama.pangolin.mybatis.mapper.hotel.HotelProductDetailMapper;
import com.lvmama.pangolin.common.RowkeyGenerator;
import com.lvmama.pangolin.entity.hotel.HotelCommodityDetail;
import com.lvmama.pangolin.entity.hotel.HotelProductDetail;
import com.lvmama.pangolin.hotel.service.HotelProductService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;


/**
 * Created by zhoutianqi on 2017/08/15.
 */
@Service
@Transactional
public class HotelProductServiceImpl implements HotelProductService {
    @Autowired
    private HotelProductDetailMapper hotelProductDetailMapper;
    @Autowired
    private HotelCommodityDetailMapper hotelCommodityDetailMapper;

	@Override
	public void saveHotelProdAndCom(HotelProductDetail prod,
			List<HotelCommodityDetail> comList,Date date)
	{
		prod.setCreateTime(null);
		int ret = hotelProductDetailMapper.updateByProd(prod);
		String prodId = prod.getProductId();
		Map<String,String> param = new HashMap<String,String>();
		param.put("productId", prodId);
		param.put("companyCode", prod.getCompanyCode());
		date = date == null ? new Date():date;
		String comrowkey = null;
		if(ret > 0)
		{
			List<HotelProductDetail> prodList  = (List<HotelProductDetail>)hotelProductDetailMapper.findListByMap(param);
			
			if(prodList != null && !prodList.isEmpty())
			{
				if(comList != null && !comList.isEmpty())
				{
					Map<String,String> paramMap = new HashMap<String, String>();
					
					for (HotelCommodityDetail commodity : comList)
					{
						
						paramMap.put("productId", commodity.getProductId());
						paramMap.put("commodityId", commodity.getCommodityId());
						//避免出现不同房型，相同商品ID
						paramMap.put("roomType", commodity.getCommodityType());
//						paramMap.put("roomType", commodity.getRoomType());
						//公司名称
						paramMap.put("companyCode", commodity.getCompanyCode());
						//从转化表中读取数据，看是否已存在
						List<HotelCommodityDetail> commList = hotelCommodityDetailMapper.findListByMap(paramMap);
						if(commList != null && !commList.isEmpty())
						{
							commodity.setId(commList.get(0).getId());
							//避免覆盖插入时间
							commodity.setCreateTime(null);
						    hotelCommodityDetailMapper.update(commodity);
						}
						else
						{
							comrowkey = RowkeyGenerator.generateCommodityRowkeyWithCompany(commodity.getCommodityId(),prod.getCompanyCode(), prod.getComeDate(), commodity.getProductId());
							commodity.setId(comrowkey);
							commodity.setCreateTime(date);
							hotelCommodityDetailMapper.save(commodity);
						}
						
					}
					
				}
			}
			
		}
		else
		{
			// 生成唯一id
			String id = RowkeyGenerator.generateProductRowkeyWithCompany(prod.getProductId(), prod.getCompanyCode(),
					prod.getComeDate(), prod.getTaskId());
			prod.setId(id);
			prod.setCreateTime(date);
			hotelProductDetailMapper.save(prod);
			if (comList != null && !comList.isEmpty())
			{
				// 默认删除该产品的商品所有数据，有无都做一次删除
				hotelCommodityDetailMapper.deleteByProdMap(param);
				for (HotelCommodityDetail commodity : comList)
				{
					comrowkey = RowkeyGenerator.generateCommodityRowkeyWithCompany(commodity.getCommodityId(),prod.getCompanyCode(), prod.getComeDate(), commodity.getProductId());
					commodity.setId(comrowkey);
					commodity.setCreateTime(date);
					hotelCommodityDetailMapper.save(commodity);
				}
			}			
			
			
		}
	}
	

}
